/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.thoughtbot.expandablecheckrecyclerview.expand;

import com.thoughtbot.expandablecheckrecyclerview.ResourceTable;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;
import ohos.app.Context;

import java.util.List;
import java.util.Map;

/**
 * ExpandListContainer
 *
 * @since 2021-07-21
 */
public class ExpandListContainer extends DirectionalLayout {
    private ListContainer listContainer;
    private Button button;
    private Component slice;
    private DirectionalLayout directionalLayout;

    private List<ExpandFatherItem> fatherData;
    private Map<String,List<ExpandChildItem>> childData;

    private ExpandFatherItem item;
    private ListContainer childListContainer;

    /**
     * ExpandListContainer 构造方法
     *
     * @param context context
     */
    public ExpandListContainer(Context context) {
        super(context);
    }

    /**
     * ExpandListContainer
     *
     * @param context context
     * @param attrSet attrSet
     */
    public ExpandListContainer(Context context, AttrSet attrSet) {
        super(context, attrSet);
        initView();
    }

    /**
     * ExpandListContainer
     *
     * @param context context
     * @param attrSet attrSet
     * @param styleName styleName
     */
    public ExpandListContainer(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
    }

    private void initView() {
        slice = LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_ability_expand_lib, this, true);
        directionalLayout = (DirectionalLayout) slice.findComponentById(ResourceTable.Id_topView);
        listContainer = (ListContainer) slice.findComponentById(ResourceTable.Id_listContainer);
        button = (Button) slice.findComponentById(ResourceTable.Id_toggle_button);
    }

    private void initData() {
        ExpandFatherProvider provider = new ExpandFatherProvider(getFatherData(),getContext());
        listContainer.setItemProvider(provider);
        listContainer.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer itemListContainer, Component component, int position, long lon) {
                controlExpand(itemListContainer,position);
            }
        });
        listContainer.setLongClickable(false);

        listContainer.setScrollListener(new ListContainer.ScrollListener() {
            @Override
            public void onScrollFinished() {
            }
        });

        button.setClickedListener(new ClickedListener() {
            @Override
            public void onClick(Component component) {
                controlExpand(listContainer,2);
            }
        });
    }

    /**
     * 控制折叠和展开
     *
     * @param itemListContainer listContainer
     * @param position position
     */
    private void controlExpand(ListContainer itemListContainer,int position) {
        item = (ExpandFatherItem)itemListContainer.getItemProvider().getItem(position);
        Image imageLabel = item.getImage();
        childListContainer = item.getListContainer();

        // 通过绑定的标签获取到对应的子组件数据
        List<ExpandChildItem> childItemList = getChildData(item.getTag());
        ChildProvider childProvider = new ChildProvider(childItemList,getContext());
        childListContainer.setItemProvider(childProvider);
        childListContainer.setLongClickable(false);

        // 控制展开或者折叠
        if (!item.isExpand()) {
            childListContainer.setVisibility(Component.VISIBLE);
            item.setExpand(true);
            startAnimation(imageLabel,-180);
        } else {
            childListContainer.setVisibility(Component.HIDE);
            item.setExpand(false);
            startAnimation(imageLabel,0);
        }
        itemListContainer.getItemProvider().notifyDataChanged();
    }

    private List<ExpandChildItem> getChildData(String fatherTag) {
        return getChildDataOfMap().get(fatherTag);
    }

    /**
     * getFatherData
     *
     * @return List
     */
    public List<ExpandFatherItem> getFatherData() {
        return fatherData;
    }

    /**
     * setFatherData
     *
     * @param fatherData fatherData
     */
    public void setFatherData(List<ExpandFatherItem> fatherData) {
        this.fatherData = fatherData;
    }

    /**
     * getChildDataOfMap
     *
     * @return Map
     */
    public Map<String, List<ExpandChildItem>> getChildDataOfMap() {
        return childData;
    }

    /**
     * setChildData
     *
     * @param childData 子组件数据
     */
    public void setChildData(Map<String, List<ExpandChildItem>> childData) {
        this.childData = childData;
        initData();
    }

    /**
     * 单独启动旋转动画
     *
     * @param component component
     * @param rotate rotate
     */
    private void startAnimation(Component component, int rotate) {
        AnimatorProperty animatorProperty = new AnimatorProperty();
        animatorProperty.setTarget(component);
        animatorProperty.rotate(rotate).setDuration(300);
        animatorProperty.setStateChangedListener(new Animator.StateChangedListener() {
            @Override
            public void onStart(Animator animator) {
            }

            @Override
            public void onStop(Animator animator) {
            }

            @Override
            public void onCancel(Animator animator) {
            }

            @Override
            public void onEnd(Animator animator) {
            }

            @Override
            public void onPause(Animator animator) {
            }

            @Override
            public void onResume(Animator animator) {
            }
        });
        animatorProperty.start();
    }

    /**
     * 单独启动透明的动画
     *
     * @param component component
     */
    private void startAlphaAnimation(Component component) {
        AnimatorProperty animatorProperty = new AnimatorProperty();
        animatorProperty.setTarget(component);
        animatorProperty.alphaFrom(0f).alpha(1.0f);
        animatorProperty.setDuration(300);
        animatorProperty.setStateChangedListener(new Animator.StateChangedListener() {
            @Override
            public void onStart(Animator animator) {
            }

            @Override
            public void onStop(Animator animator) {
            }

            @Override
            public void onCancel(Animator animator) {
            }

            @Override
            public void onEnd(Animator animator) {
            }

            @Override
            public void onPause(Animator animator) {
            }

            @Override
            public void onResume(Animator animator) {
            }
        });
        animatorProperty.start();
    }
}
